package com.common.utils.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.apache.commons.lang3.StringUtils;

import android.content.Context;

public class TextFileManager {
	
	public static String readTextFile(File file) {
		try {
			BufferedReader buffreader = new BufferedReader(new FileReader(file));
			try {
				String currentLine;
				StringBuilder builderStr = new StringBuilder();

				while ((currentLine = buffreader.readLine()) != null) {
					builderStr.append(currentLine);
					builderStr.append(System.getProperty("line.separator"));
				}
				return builderStr.toString();
			} finally {
				buffreader.close();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public File saveFile(Context app, String filePath, String fileContent) {
		OutputStreamWriter outFile;
		
		if(StringUtils.isNotEmpty(fileContent)) {
			try {
				outFile = new OutputStreamWriter(app.openFileOutput(filePath,0));
				outFile.write(fileContent);
				outFile.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}
		return new File(filePath);
	}
}
